let currentPath = process.argv.splice(2)[0].replace('\\','/').replace('\\','/');
const chokidar = require('chokidar');
const fs = require('fs');
const path = require('path');
const { Subject } = require('rxjs');
const { debounceTime } = require('rxjs/operators');
let sub = new Subject();
let set = new Set();
chokidar.watch(['./src', './*.js']).on('all', (event, filePath) => {
    if(set.has(filePath) == false) {
        set.add(filePath);
        return;
    }
    if(filePath.indexOf('src') == -1 && filePath.indexOf('./js') == -1) {
        return;
    };
    sub.next(path.resolve(filePath));
});
sub.pipe(debounceTime(500)).subscribe(res => {
    console.log('%c--------------------------------------------------------------------------------', "color: #00FF00;background-color: #00FF00;weight: blod;");
    try {
        delete require.cache[res];
    } catch (error) {
        console.error(error)
    }
    try {
        let moduleCode = require(res);
        let date = new Date();
        let result = moduleCode();
        console.log(result);
        time =  new Date().getTime() - date.getTime()+'ms';
        console.log('%c'+time, 'color: #43bb88;font-size: 24px;font-weight: bold;text-decoration: underline;');
    } catch (error) {
        console.error(error)
    }
    
});
